package de.pipeGame.playingField;

import de.pipeGame.GamePreset;
import de.pipeGame.R;
import de.pipeGame.R.drawable;
import de.pipeGame.itemStack.ItemStackHolder;
import EventSourcing.DragComponent;
import EventSourcing.EventQueue;
import EventSourcing.PipeDragEvent;
import EventSourcing.TrackableEvent;
import android.content.ClipData;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

/**
 * One piece of the playing field. It may contain an image and feels itself as a drop target
 * @author Anthea
 *
 */
public class PlayingFieldItem implements DragComponent {

	public final static int NO_ITEM = -1;
	int pos_x;
	int pos_y;
	ImageView iv;
	int itemContained = -1;
	ItemStackHolder itemStackHolder;
	int width;
	int height;
	boolean isStart;
	boolean isTarget;
	EventQueue eventQueue;

	PlayingFieldItem(int _pos_x, int _pos_y, ImageView iv,
			ItemStackHolder itemStackHolder, int height, int width, EventQueue eventQueue) {
		this.pos_x = _pos_x;
		this.pos_y = _pos_y;
		this.iv = iv;
		this.itemStackHolder = itemStackHolder;
		this.width = width;
		this.height = height;
		this.isStart = false;
		this.isTarget = false;
		this.eventQueue= eventQueue;

		// Creates a new drag event listener
		View.OnDragListener mDragListener = new myDragEventListener();
		iv.setOnDragListener(mDragListener);

	}

	private class myDragEventListener implements View.OnDragListener {

		public boolean onDrag(View view, DragEvent dragEvent) {
			int dragAction = dragEvent.getAction();

			if ((dragAction == DragEvent.ACTION_DROP) && isDropTarget()) {
				ClipData.Item item = dragEvent.getClipData().getItemAt(0);
				Log.d("PIPEGAME", "" + item.getText());
				int dragItem = Integer.parseInt(item.getText().toString());
					if (dragItem != NO_ITEM && itemContained==NO_ITEM) {
						TrackableEvent te =new PipeDragEvent((DragComponent)itemStackHolder.get(dragItem),PlayingFieldItem.this, dragItem);
						te.executeEvent();
						eventQueue.add(te);
					}				
			}
			return true;
		}
	}

	public boolean isDropTarget() {
		return ((!isStart) && (!isTarget));
	}

	/**
	 * sets this item as start point
	 * 
	 * @param dir
	 *            indicated the direction (TODO)
	 */
	public void setStart(int dir) {
		this.iv.setImageResource(R.drawable.pipe_item_start_s);
		this.isStart = true;

	}

	/**
	 * sets this item as target point
	 * 
	 * @param dir indicated the direction (TODO)
	 */
	public void setTarget(int dir) {
		this.iv.setImageResource(R.drawable.ic_launcher);
		this.isTarget = true;
	}

	public void clear() {
		iv.setImageDrawable(null);
		iv.invalidate();
		
	}

	public void performDragTargetEvent(int dragItem) {
		this.itemContained=dragItem;
		iv.setImageResource(GamePreset.getDrawableForPipeType(itemContained));
		iv.setLayoutParams(new FrameLayout.LayoutParams(width,height));
	}

	public void performDragSourceEvent() {
		this.clear();
		itemContained =NO_ITEM;
	}
}
